package com.guigu.pd.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigu.pd.auth.entity.CoreOrgEntity;
import com.guigu.pd.auth.service.CoreOrgService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.auth.dao.AuthUserDao;
import com.guigu.pd.auth.entity.AuthUserEntity;
import com.guigu.pd.auth.service.AuthUserService;


@Service("authUserService")
public class AuthUserServiceImpl extends ServiceImpl<AuthUserDao, AuthUserEntity> implements AuthUserService {


    @Autowired
    private CoreOrgService coreOrgService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AuthUserEntity> page = this.page(
                new Query<AuthUserEntity>().getPage(params),
                new QueryWrapper<AuthUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils kdylistqueryPage(Map<String, Object> params) {
        AuthUserEntity authUser = JSON.parseObject(JSON.toJSONString(params),
                AuthUserEntity.class);

        LambdaQueryWrapper<AuthUserEntity> queryWrapper = new LambdaQueryWrapper<AuthUserEntity>();
        queryWrapper.eq( AuthUserEntity::getStationId, 3);

        queryWrapper.like(StringUtils.isNotBlank(authUser.getName()),
                AuthUserEntity::getName, authUser.getName());

        queryWrapper.like(StringUtils.isNotBlank(authUser.getMobile()),
                AuthUserEntity::getMobile, authUser.getMobile());

        IPage<AuthUserEntity> page = this.page(
                new Query<AuthUserEntity>().getPage(params),
                queryWrapper
        );

        List<AuthUserEntity> authUserEntities = page.getRecords();
        for (AuthUserEntity authUserEntity : authUserEntities) {
            //获取快递员所属网点
            CoreOrgEntity byId = coreOrgService.getById(authUserEntity.getOrgId());
            authUserEntity.setOrgName(byId.getName());
        }

        return new PageUtils(page);
    }

}